package org.csu.mypetstore.controller.user;

import org.csu.mypetstore.common.CommonResponse;
import org.csu.mypetstore.domain.PO.*;
import org.csu.mypetstore.domain.BO.AccountBO;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.Impl.AccountServiceImpl;
import org.csu.mypetstore.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class AccountController {

    @Autowired
    AccountService accountService;
    @Autowired
    HttpServletRequest httpServletRequest;

    @ResponseBody
    @GetMapping("/accounts/{username}")
    public CommonResponse<Account> getAccount(@PathVariable String username, String action) {
        if (action != null)//过滤到这里只要有action则必定不为空
            return CommonResponse.createForSuccessMessage(accountService.isUsernameExist(username) ? "用户已存在" : "用户不存在");
        else{//获取request的头部token信息
            if (username.equals(TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"), "username")))//如果用户名和请求信息的用户名一致才回复
                return CommonResponse.createForSuccess(accountService.getAccountByUsername(username), "获取用户信息成功");
            return CommonResponse.createForError("无法获取他人的信息!");
        }
    }

    @ResponseBody
    @PostMapping("/tokens")
    public CommonResponse<Map<String, String>> getTokens(String username, String password){
        String token = accountService.getToken(username, password);
        if (token == null)
            return CommonResponse.createForError("登陆失败，未知错误");
        Map<String, String> tokenMap = new HashMap<>();//将token打包到map中进行返回
        tokenMap.put("token", token);
        return CommonResponse.createForSuccess(tokenMap, "登陆成功");
    }

    @ResponseBody
    @PutMapping("/accounts/{username}")
    public CommonResponse<Account> editAccount(@PathVariable String username, AccountBO accountBO, String action){
        if (!username.equals(TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"), "username")))
            return CommonResponse.createForError("不能修改他人的信息！");
        if ("cancel".equals(action)){//如果是注销账户
            return CommonResponse.createForSuccess(accountService.cancelAccount(accountService.getAccountByUsername(username)),
                    "注销成功");
        }else if (action == null){//如果没有action字段就是单纯的编辑修改
            accountBO.setUsername(username);
            accountService.updateAccount(accountBO);
            return CommonResponse.createForSuccess(AccountServiceImpl.getAccountFromAccountBO(accountBO), "修改信息成功");
        }else//如果有action字段但不是cancel操作
            return CommonResponse.createForError("未知错误");
    }

    @ResponseBody
    @PostMapping("/accounts")
    public CommonResponse<Account> register(AccountBO accountBO){
        accountService.insertAccount(accountBO);
        return CommonResponse.createForSuccess(AccountServiceImpl.getAccountFromAccountBO(accountBO), "注册成功");
    }
}
